package com.yrh.dao.impl;

import com.yrh.bean.TAddress;
import com.yrh.dao.TAddressDao;
import com.yrh.utils.DataSourceUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/*地址数据层接口实现类*/
public class TAddressDaoImpl implements TAddressDao {


    //创建jdbc操作类
    private JdbcTemplate temp=new JdbcTemplate(DataSourceUtil.getDataSource());
    //查询所有地址
    @Override
    public List<TAddress> allAddress(TAddress tAddress) {
        Object [] objects=new Object[]{ tAddress.getUserId()
        };
        try {
            return temp.query("SELECT * FROM t_address WHERE user_id=?",objects,new BeanPropertyRowMapper<>(TAddress.class));
        } catch (Exception e) {
           return null;
        }
    }
    /*修改默认地址*/
    @Override
    public int updateDefault(TAddress tAddress) {
        if (tAddress.getAddressId()==0){
            return temp.update("UPDATE t_address SET address_default=?","N");
        }else {
            return temp.update("UPDATE t_address SET address_default=? WHERE address_id=?","Y",tAddress.getAddressId());
        }

    }
    //删除地址
    @Override
    public int delAddress(TAddress tAddress) {
        return temp.update("delete from t_address where address_id=?",tAddress.getAddressId());
    }
    //修改地址
    @Override
    public int updateAds(TAddress tAddress) {
        Object []objects=new Object[]{
                tAddress.getAddressProvince(),
                tAddress.getAddressCity(),
                tAddress.getAddressDistrict(),
                tAddress.getAddressDescribe(),
                tAddress.getAddressId()
        };
        return temp.update("update t_address set address_province=?,address_city=?,address_district=?,address_describe=? where address_id=?",objects);
    }
/*添加地址*/
    @Override
    public int addAddress(TAddress tAddress) {
        Object []objects=new Object[]{
                tAddress.getAddressProvince(),
                tAddress.getAddressCity(),
                tAddress.getAddressDistrict(),
                tAddress.getAddressDescribe(),
                tAddress.getUserId()
        };
        return temp.update("insert into t_address(address_province,address_city,address_district,address_describe,user_id) values(?,?,?,?,?)",objects);
    }

}
